
let Methods = require('./methods');

/**
 * 唯一解法提示
 * @param board
 * @return {*}
 */
function unique(boardInfo) {

    let board = boardInfo.board;
    let obj = {};
    for(let i = 1; i <= boardInfo.boardSize;++i){
        obj[i] = true;
    }

    // 检查行是否有唯一解
    for(let i = 0; i < board.length; ++i){
        let zeroCount = 0;
        let zeroJ = -1;
        for(let j = 0; j < board[0].length; ++j){
            if(board[i][j] === 0){
                zeroCount++;
                zeroJ = j;
            }
        }
        if(zeroCount === 1){
            for(let s = 0;s < board[0].length;++s){
                let value = board[i][s];
                if(value > 0){
                    obj[value] = false;
                }
            }
            let value;
            for(let k in obj){
                if(obj[k]){
                    value = Number(k);
                }
            }
            return {x:i,y:zeroJ,type:Methods.Unique.Row,value:value}
        }
    }


    // 检查列是否有唯一解
    for(let j = 0; j < board[0].length;++j){
        let zeroCount = 0;
        let zeroI = -1;
        for(let i = 0; i < board.length;++i){
            if(board[i][j] === 0){
                zeroCount++;
                zeroI = i;
            }
        }
        if(zeroCount === 1){
            for(let i = 0; i < board.length;++i){
                let value = board[i][j];
                if(value > 0){
                    obj[value] = false;
                }
            }
            let value;
            for(let k in obj){
                if(obj[k]){
                    value = Number(k);
                }
            }
            return {x:zeroI,y:j,type:Methods.Unique.Col,value:value}
        }
    }

    // 检查宫内是否有唯一解
    let gongCount = boardInfo.gongCount;
    let gongRowCount = boardInfo.gongRowCount;
    let gongColCount = boardInfo.gongColCount;
    for(let k = 0; k < gongCount; ++k){
        let zeroCount = 0;
        let zeroI = -1;
        let zeroJ = -1;
        let ids = boardInfo.getGongStartIdx(k);
        for(let i = 0;i < gongRowCount;++i){
            for(let j = 0; j < gongColCount;++j){
                let ii = ids.x + i;
                let jj = ids.y + j;
                if(board[ii][jj] === 0){
                    zeroCount++;
                    zeroI = ii;
                    zeroJ = jj;
                }
            }
        }
        if(zeroCount === 1){
            for(let i = 0;i < gongRowCount;++i){
                for(let j = 0; j < gongColCount;++j){
                    let ii = ids.x + i;
                    let jj = ids.y + j;
                    let value = board[ii][jj];
                    if(value > 0){
                        obj[value] = false;
                    }
                }
            }
            let value;
            for(let k in obj){
                if(obj[k]){
                    value = Number(k);
                }
            }
            return {x:zeroI,y:zeroJ,type:Methods.Unique.Gong,value:value}
        }
    }

    return null;
}

module.exports = unique;
